﻿using System;
using System.Collections.Generic;
using System.Linq;
using Luna.Data.Test;
using NUnit.Framework;

namespace Luna.Data.Budget
{
    internal class BudgetTest : DataTest<IBudgetProvider, IBudgetData>
    {
        private const string COMMENT = "Commen";

        private const string NAME = "Name";

        public BudgetTest(string contextName)
            : base(contextName)
        {
        }

        public override Action<Guid> Delete
        {
            get { return Service.BudgetDelete; }
        }

        public override Predicate<IBudgetData> DeletePredicate
        {
            get { return x => x.Name.StartsWith(TEST_PREFIX); }
        }

        public override Func<Guid, IBudgetData> Get
        {
            get { return Service.BudgetGet; }
        }

        public override Func<IEnumerable<IBudgetData>> GetAll
        {
            get { return Service.BudgetGetAll; }
        }

        public override Action<IBudgetData> Save
        {
            get { return (x) => Service.BudgetSave(x, null); }
        }

        public override bool AreSame(IBudgetData P_Entity1, IBudgetData P_Entity2)
        {
            bool L_Return = true;

            L_Return &= P_Entity1.PK == P_Entity2.PK;
            L_Return &= P_Entity1.Name == P_Entity2.Name;
            L_Return &= P_Entity1.Comment == P_Entity2.Comment;
            L_Return &= P_Entity1.MainBudgetID == P_Entity2.MainBudgetID;
            L_Return &= P_Entity1.Start == P_Entity2.Start;
            L_Return &= P_Entity1.End == P_Entity2.End;
            L_Return &= P_Entity1.LastVersion == P_Entity2.LastVersion;
            L_Return &= P_Entity1.Validated == P_Entity2.Validated;

            return L_Return;
        }

        public override IBudgetData Create()
        {
            IBudgetData L_Entity = base.Create();
            L_Entity.PK = Guid.NewGuid();
            L_Entity.Name = GetString(NAME);
            L_Entity.Comment = GetString(COMMENT);
            L_Entity.MainBudgetID = Guid.NewGuid();
            L_Entity.Start = GetDate();
            L_Entity.End = GetDate();
            L_Entity.LastVersion = GetBoolean();
            L_Entity.Validated = GetBoolean();
            return L_Entity;
        }

        public override void CreateTestData()
        {
            var L_TestData = Create();
            L_TestData.PK = BUDGET;
            Save(L_TestData);
        }

        [Test]
        public void TestGetAllByMainID()
        {
            IBudgetData L_Entity = Create();
            IBudgetData L_Entity_Modified = Create();
            L_Entity_Modified.MainBudgetID = L_Entity.MainBudgetID;
            Save(L_Entity);
            Save(L_Entity_Modified);

            var L_Return = Service.BudgetGetAllByMainBudget(L_Entity_Modified.MainBudgetID);
            Expect(L_Return.Count(), Is.EqualTo(2));
        }
    }
}